package cn.org.donald.framework.handler;

import cn.org.donald.service.system.TableFileService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author : Donald
 * @since : 2020/12/14 16:17
 */
@Component
public class CleanFileExecutor {

    @Reference(version = "1.0.0",group = "donald")
    private TableFileService tableFileService;

    private static final Logger logger = LoggerFactory.getLogger(CleanFileExecutor.class);

    /**
     * 根据用户id清理文件调度任务
     * @param msg
     * @return
     * @throws Exception
     */
    @XxlJob("cleanFileByUserIdExecutor")
    public ReturnT<String> cleanFileByUserIdExecutor(String msg) throws Exception{
        return getLogger(msg, tableFileService.cleanFileByUserId());
    }



    /**
     * 根据文件名清理文件调度任务
     * @param msg
     * @return
     * @throws Exception
     */
    @XxlJob("cleanFileByFileNameExecutor")
    public ReturnT<String> cleanFileByFileNameExecutor(String msg) throws Exception{
        return getLogger(msg, tableFileService.cleanFileByFileName());
    }


    private ReturnT<String> getLogger(String msg, int rows) {
        logger.info("executor is running, from {}{}",this.getClass().getName(),msg==null?"":",task msg: "+msg);
        logger.info("Executor {} run successfully, has deleted {} files",this.getClass().getName(), rows);
        XxlJobLogger.log("has deleted" + rows + " files");
        return ReturnT.SUCCESS;
    }
}
